<template>
  <div class="category-label" :title="text">
    <span>
      <a-tag
        v-if="showCategory.openLink || showCategory.openCtpWindow"
        :style="categoryStyle"
        @click.stop="openUrl"
        class="tag-show">
        {{ text }}
      </a-tag>
      <a-tag v-else :style="categoryStyle" class="tag-show">
        {{ text }}
      </a-tag>
    </span>
  </div>
</template>

<script>
  /*global _ctxPath */
  export default {
    name: 'CategoryLabel',
    props: {
      slotData: {
        type: Object,
        default: () => {}
      },
      urlParams: {
        type: Object,
        default: () => {}
      },
      isRegistRole: {
        type: String,
        default: ''
      },
      text: {
        type: String,
        default: ''
      }
    },
    computed: {
      showCategory() {
        const { applicationCategoryKey, hasResPerm, categoryOpenType, thirdApp } = this.slotData;
        const openMethod = {
          openCtpWindow: false,
          openLink: false
        };
        if (
          (applicationCategoryKey === 24 && !this.isRegistRole) ||
          this.urlParams.from === 'Agent'
        ) {
          return openMethod;
        } else {
          if (hasResPerm) {
            if (categoryOpenType == 3) {
              openMethod.openCtpWindow = true;
            } else {
              if (applicationCategoryKey !== 81 && applicationCategoryKey !== 39 && !thirdApp) {
                openMethod.openLink = true;
              }
            }
          }
          return openMethod;
        }
      },
      categoryStyle() {
        const { applicationCategoryKey } = this.slotData;
        //与公文颜色一致的app类型,均为公文相关
        // 4公文
        // 19发文
        // 20 收文
        // 21 签报
        // 24 待登记公文
        // 22 待发送公文
        // 23 待签收公文
        // 16 交换
        // 34 收文分发•公文
        // 401 表单公文-发文
        // 402 表单公文-收文
        // 403 表单公文-交换公文
        // 404 表单公文-签报
        const govColor = [4, 19, 20, 21, 24, 22, 23, 16, 34, 401, 402, 403, 404];
        //6 会议、29会议室
        const meetingColor = [6, 29];
        // 32 信息报送、33 信息报送统计
        const messageColor = [32, 33];
        //7 公告 8 新闻 10 调查
        const cultureColor = [7, 8, 10];
        //1 协同
        const collaborationColor = 1;
        //26 综合办公
        const officeColor = 26;

        let showColorKey;
        if (
          applicationCategoryKey === collaborationColor ||
          applicationCategoryKey === officeColor
        ) {
          showColorKey = applicationCategoryKey;
        } else if (govColor.includes(applicationCategoryKey)) {
          showColorKey = 4;
        } else if (meetingColor.includes(applicationCategoryKey)) {
          showColorKey = 6;
        } else if (messageColor.includes(applicationCategoryKey)) {
          showColorKey = 32;
        } else if (cultureColor.includes(applicationCategoryKey)) {
          showColorKey = 7;
        } else {
          showColorKey = 'other';
        }
        return {
          color: this.colorArray[showColorKey]?.fontColor,
          backgroundColor: this.colorArray[showColorKey]?.backgroundColor,
          opacity: this.colorArray[showColorKey]?.opacity
        };
      }
    },
    created() {
      this.colorArray = {
        1: { fontColor: '#305BF9', backgroundColor: '#EBEFFE' }, //协同
        4: { fontColor: '#EF5350', backgroundColor: 'rgba(239, 83, 80, 0.1)' }, //公文
        6: { fontColor: '#FF9F00', backgroundColor: '#FFF4DA' }, //会议
        7: { fontColor: '#7CB342', backgroundColor: 'rgba(124, 179, 66, 0.1)' }, //调查
        26: { fontColor: '#A71EEB', backgroundColor: 'rgba(167, 30, 235, 0.1)' }, //综合办公
        // 39: { fontColor: '#26A69A', backgroundColor: 'rgba(38, 166, 154, 0.1)' }, //三方
        32: { fontColor: '#00ACC1', backgroundColor: 'rgba(0, 172, 193, 0.1)' },
        other: { fontColor: '#26A69A', backgroundColor: 'rgba(38, 166, 154, 0.1)' }
      };
      this.importantLevelText = {
        2: $.i18n('govdoc.quick.normal'),
        3: $.i18n('govdoc.quick.expedited'),
        4: $.i18n('govdoc.quick.express'),
        5: $.i18n('govdoc.quick.teti')
      };
    },
    methods: {
      openLink() {
        this.$emit('openLink', this.slotData);
      },
      openUrl() {
        if (this.showCategory.openCtpWindow) {
          window.openCtpWindow({ url: `${_ctxPath}${this.slotData.categoryLink}` });
        } else if (this.showCategory.openLink) {
          this.openLink(`${this.slotData.categoryLink}`, `${this.slotData.openType}`);
        } else {
          return;
        }
      }
    }
  };
</script>

<style scoped lang="scss">
  .category-label {
    width: 100%;
    // white-space: nowrap;
    // text-overflow: ellipsis;
    // overflow: hidden;
    .tag-show {
      max-width: 100%;
      border-radius: 2px;
      vertical-align: middle;
      border: none;
      cursor: pointer;
      white-space: nowrap;
      text-overflow: ellipsis;
      overflow: hidden;
      font-weight: initial;
    }
  }
</style>
